/*
 * @Author: 王雁鹏
 * @Date: 2023-06-15 22:22:48
 * @LastEditTime: 2023-07-31 17:21:30
 * @LastEditors: Please set LastEditors
 * @Description: 
 * @FilePath: \expressf:\web-vue\bs-nodejs-background-code\routes\user\service.js
 * -------------------------------------
 */
const request = require('request');
var req = require("../../utils/router");
const db = require("../../mysql/index.js");
const config = require("./config.js");
const { generateUUID, createToken } = require("../../utils/func.js")
let { router, GetRouter, PostRouter } = req
// 设置菜单
new PostRouter().send('/login', function (res, data) {
    let { username, password } = data
    if (!username || !password) {
        res.send({ code: 100, data: {}, msg: "没有该账号" });
        return
    }
    let obj = {
        username: db.EQ({ ...config, select: { username } }),
        password: db.EQ({ ...config, select: { password } })
    }
    let select = db.Params(obj)
    db.select({
        ...config,
        select
    }, (rows, fields) => {
        if (rows.list.length > 0) {
            let code = rows.list[0].code
            let token = createToken({ username, code })
            res.send({ code: 200, data: { code, token } });
        } else {
            res.send({ code: 100, data: {}, msg: "没有该账号" });
        }

    })
})

new PostRouter().send('/userList', function (res, data) {
    let { pageSize, pageNum, phone } = data
    let obj = phone ? {
        phone: db.LIKE({ db: "wh_users", select: { phone } }),
    } : {}
    let select = db.Params(obj)
    console.log(select, "select", phone, obj)
    db.select({
        db: "wh_users",
        select,
        pageSize, pageNum,
    }, (rows, fields) => {
        let arr = rows.list.map(i => {
            return {
                phone: i.phone || "",
                score: i.score
            }
        })
        res.send({ code: 200, data: { total: rows.total, list: arr } });
    })
})

// 设置菜单
new PostRouter().send('/register', function (res, data) {
    let { name, phone, username, password, } = data
    let condition = { name, code: generateUUID() }
    db.setData({
        ...config,
        id,
        condition
    }, (rows, fields) => {
        res.send({ code: 200, data: rows });
    })
})

module.exports = router